package cn.fanyamin.web.struts;


import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.fanyamin.business.TaskService;
import cn.fanyamin.domain.Task;
import cn.fanyamin.domain.User;
import cn.fanyamin.util.CalendarUtils;
import cn.fanyamin.util.collection.CollectionUtils;

public class CalendarAction extends SecureBaseAction {

    public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) {
        TaskService taskService = getWebApp().getTaskService();
        
        //String calType = request.getParameter("calType");
          
        
        String strDate = request.getParameter("period");
        Date now = new Date();
        
        if(StringUtils.isNotBlank(strDate)) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            
            try {
                now = sdf.parse(strDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        
        User user = super.getUserFromSession(request);
        
        Map<Date,List<Task>> taskMap = null;
        
        /*if(StringUtils.equals("weekly", calType)) {
        	long minTS = CalendarUtils.getMinDateOfWeek(now).getTime();
        	List<Task> taskList = taskService.getTaskList(new Date(minTS)
    		, CalendarUtils.getMaxDateOfWeek(now), user.getUserID());
        	taskMap = makeWeeklyMap(minTS, taskList);
        } else if(StringUtils.equals("monthly", calType)) {
        	long minTS = CalendarUtils.getMinDateOfMonth(now).getTime();
        	List<Task> taskList = taskService.getTaskList(new Date(minTS)
    		, CalendarUtils.getMaxDateOfMonth(now), user.getUserID());
        	taskMap = makeMonthlyMap(minTS, taskList);
        } else {//daily
*/        	long minTS = CalendarUtils.getMinDateOfDay(now).getTime();
        	List<Task> taskList = taskService.getTaskList(new Date(minTS)
    		, CalendarUtils.getMaxDateOfDay(now), user.getUserID());
        	taskMap = makeDailyMap(minTS, taskList);
        
        	List<Task> todoList = taskService.getTaskListByDeadline(new Date(minTS)
            , CalendarUtils.getMaxDateOfDay(now), user.getUserID());
            
        //}
        request.setAttribute("todoList", todoList);
        request.setAttribute("curMS", now.getTime());
        request.setAttribute("taskMap", taskMap);
       
        return new ActionForward("/WEB-INF/jsp/struts/task/calendar.jsp");

    }

	
	public static Map<Date,List<Task>> makeDailyMap(long minTS, List<Task> taskList) {
		Map<Date,List<Task>> taskMap = new TreeMap<Date,List<Task>>();
		for (int i = 0; i < CalendarUtils.HOUR_OF_DAY; i++) {
			taskMap.put( new Date(minTS + i * CalendarUtils.MS_OF_HOUR)
				, new ArrayList<Task>(3));
		}
		if(CollectionUtils.isEmpty(taskList)) {
			return taskMap;
		}
        for(Task task: taskList) {
        	Timestamp ts = task.getBeginTime();
        	if(ts == null) {
        		continue;
        	}
        	long ms = ts.getTime();
        	int ix = (int)((ms - minTS)/CalendarUtils.MS_OF_HOUR);
        	Date aDate = new Date(minTS + ix * CalendarUtils.MS_OF_HOUR);
        	List<Task> aList = taskMap.get(aDate);
        	if(aList == null) {
        		aList = new ArrayList<Task>(3);
        		taskMap.put(aDate,aList);
        	}
        	aList.add(task);
        }
		return taskMap;
	}
	
	public static Map<Date,List<Task>> makeWeeklyMap(long minTS, List<Task> taskList) {
		Map<Date,List<Task>> taskMap = new TreeMap<Date,List<Task>>();
		for (int i = 0; i < CalendarUtils.HOUR_OF_WEEK; i++) {
			taskMap.put( new Date(minTS + i * CalendarUtils.MS_OF_DAY)
				, new ArrayList<Task>(3));
		}
		if(CollectionUtils.isEmpty(taskList)) {
			return taskMap;
		}
        for(Task task: taskList) {
        	Timestamp ts = task.getBeginTime();
        	if(ts == null) {
        		continue;
        	}
        	long ms = ts.getTime();
        	int ix = (int)((ms - minTS)/CalendarUtils.MS_OF_HOUR);
        	Date aDate = new Date(minTS + ix * CalendarUtils.MS_OF_HOUR);
        	List<Task> aList = taskMap.get(aDate);
        	if(aList == null) {
        		aList = new ArrayList<Task>(3);
        		taskMap.put(aDate,aList);
        	}
        	aList.add(task);
        }
		return taskMap;
	}
	
	public static Map<Date,List<Task>> makeMonthlyMap(long minTS, List<Task> taskList) {
		Map<Date,List<Task>> taskMap = new TreeMap<Date,List<Task>>();

		
		for (int i = 0; i < CalendarUtils.getMaxDayOfMonth(new Date(minTS)); i++) {
			taskMap.put( new Date(minTS + i * CalendarUtils.MS_OF_DAY)
				, new ArrayList<Task>(3));
		}
		if(CollectionUtils.isEmpty(taskList)) {
			return taskMap;
		}
        for(Task task: taskList) {
        	Timestamp ts = task.getBeginTime();
        	if(ts == null) {
        		continue;
        	}
        	long ms = ts.getTime();
        	int ix = (int)((ms - minTS)/CalendarUtils.MS_OF_DAY);
        	Date aDate = new Date(minTS + ix * CalendarUtils.MS_OF_DAY);
        	List<Task> aList = taskMap.get(aDate);
        	if(aList == null) {
        		aList = new ArrayList<Task>(3);
        		taskMap.put(aDate,aList);
        	}
        	aList.add(task);
        }
		return taskMap;
	}
}
